<?php

namespace App\Http\Controllers\Home;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Models\Attr;
use App\Http\Controllers\Controller;

class GoodsControllers extends Controller
{
    public function allClass(Request $request)
    {
        $path = $request->get('path');
        $pathClass = $this->path($path);
        $thisCname = Category::where('path',$path)->select('cname')->get();
        //通过获取到的父类path匹配所有的子类path
        $cname = Category::where('path','like',$pathClass.'%')->get();
        $id = $request->get('id');
        //用ID匹配对应的cid，在用关联属性取到attrValue里面对应的值
        $title = Attr::where('cid',$id)->with('attrValue')->get();
        $attrs = [];
        foreach ($title as $attr){
            $attrs[] = $attr;
        }
        return view('home/goods/all_class')->with('attrs',$attrs)->with('cname',$cname)->with('thisCname',$thisCname);
    }

    private function path($path)
    {
        $pathClass = substr($path,0,3);
        return $pathClass;
    }

    public function sku(Request $request)
    {
        $skus = $request->get('sku');
    }
}